package com.linkwechat.web.controller.wecom;

import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.entity.WeCorpAccount;
import com.linkwechat.common.enums.MediaType;
import com.linkwechat.common.tenant.TenantContextHolder;
import com.linkwechat.common.utils.ServletUtils;
import com.linkwechat.wecom.client.EsbClient;
import com.linkwechat.wecom.client.WeKfClient;
import com.linkwechat.wecom.client.WeUserClient;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.WeworkKfAccount;
import com.linkwechat.wecom.domain.dto.WeMediaDto;
import com.linkwechat.wecom.domain.dto.WeUserDto;
import com.linkwechat.wecom.domain.dto.kf.*;
import com.linkwechat.wecom.mapper.WeCorpAccountMapper;
import com.linkwechat.wecom.mapper.WeUserMapper;
import com.linkwechat.wecom.mapper.WeworkKfAccountMapper;
import com.linkwechat.wecom.service.IWeMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2022-2-18.
 */
@Slf4j
@RestController
@RequestMapping("/wecom/kf")
public class WeworkKfController extends BaseController {
    @Resource
    WeworkKfAccountMapper weworkKfAccountMapper;

    @Resource
    WeCorpAccountMapper weCorpAccountMapper;

    @Resource
    WeUserMapper weUserMapper;

    @Autowired
    WeKfClient weKfClient;

    @Autowired
    EsbClient esbClient;

    @Autowired
    WeUserClient weUserClient;

    @Autowired
    private IWeMaterialService materialService;



    //验证及同步企微客服账号与接待人员
    @PostMapping(value = "/syncWeworkKf")
    public void syncWeworkKf(@RequestBody SyncWeworkKfDto syncWeworkKfDto) {
        // 进行租户判断
        String tenantCode= syncWeworkKfDto.getTenantCode();
        if(tenantCode==null || tenantCode.trim().equals("")){
            tenantCode="0";
        }
        TenantContextHolder.setTenantCode(tenantCode);

        //获取企业信息
        WeCorpAccount weCorpAccount = weCorpAccountMapper.selectOne(null);

        WeMediaDto mediaDto = materialService.uploadTemporaryMaterial("https://wwcdn.weixin.qq.com/node/wework/images/kf_head_image_url_2.png", MediaType.IMAGE.getMediaType(), "临时文件");

        //接口返回实体
        AddAccountDto addAccountDto = new AddAccountDto();
        AddAccountResDto addAccountResDto;
        AddContactWayDto addContactWayDto = new AddContactWayDto();
        AddContactWayResDto addContactWayResDto;

        //客服账号名称
        String kfAccountName;
        if(syncWeworkKfDto.getCompanyName().length()>16) {    //账号名称不能超过16
            kfAccountName = syncWeworkKfDto.getCompanyName().substring(0, 16);
        } else {
            kfAccountName = syncWeworkKfDto.getCompanyName();
        }

        WeKfSyncContactWayDto weKfSyncContactWayDto = new WeKfSyncContactWayDto();
        WeworkKfAccount weworkKfAccount = new WeworkKfAccount();
        weworkKfAccount.setScene(syncWeworkKfDto.getCompanyId().toString());
        List<WeworkKfAccount> weworkKfAccounts = weworkKfAccountMapper.selectByScene(weworkKfAccount);
        if (weworkKfAccounts.size() == 0 || weworkKfAccounts.get(0).getState()==0) {
            //注册客服账号
            addAccountDto.setName(kfAccountName);
            addAccountDto.setMedia_id(mediaDto.getMedia_id());
            addAccountResDto = weKfClient.addAccount(addAccountDto);

            //记录账号信息
            if(weworkKfAccounts.size()==0) {
                weworkKfAccount.setOpenKfid(addAccountResDto.getOpenKfid());
                weworkKfAccount.setKfAccountName(kfAccountName);
                weworkKfAccount.setState(1);
                weworkKfAccount.setServiceNum(0);
                weworkKfAccount.setCreateTime(new Date());
                weworkKfAccountMapper.insertWeworkKfAccount(weworkKfAccount);

            } else {
                weworkKfAccount = weworkKfAccounts.get(0);
                weworkKfAccount.setOpenKfid(addAccountResDto.getOpenKfid());
                weworkKfAccount.setKfAccountName(kfAccountName);
                weworkKfAccount.setState(1);
                weworkKfAccountMapper.updateById(weworkKfAccount);
            }

            //获取客服账号链接并记录
            addContactWayDto.setOpen_kfid(addAccountResDto.getOpenKfid());
            addContactWayDto.setScene(syncWeworkKfDto.getCompanyId().toString());
            addContactWayResDto = weKfClient.addContactWay(addContactWayDto);

            //记录客服账号链接
            weKfSyncContactWayDto.setCompanyId(syncWeworkKfDto.getCompanyId());
            weKfSyncContactWayDto.setKfUrl(addContactWayResDto.getUrl());
            weKfSyncContactWayDto.setCorpId(weCorpAccount.getCorpId());
            esbClient.syncContactWay(weKfSyncContactWayDto);

        } else {
            weworkKfAccount = weworkKfAccounts.get(0);
            weworkKfAccount.setKfAccountName(kfAccountName);
            weworkKfAccountMapper.updateById(weworkKfAccount);
        }

    }

    //获取企微个人二维码
    @PostMapping(value = "/getWeworkQrUrl")
    public GetWeworkQrUrlResDto getWeworkQrUrl(@RequestBody SyncWeworkKfDto syncWeworkKfDto) {
        GetWeworkQrUrlResDto getWeworkQrUrlResDto = new GetWeworkQrUrlResDto();

        // 进行租户判断
        String tenantCode= syncWeworkKfDto.getTenantCode();
//        if(tenantCode==null || tenantCode.trim().equals("")){
//            tenantCode="0";
//        }
        TenantContextHolder.setTenantCode(tenantCode);

        //获取企业信息
        //WeCorpAccount weCorpAccount = weCorpAccountMapper.selectOne(null);

        WeUser weUser = new WeUser();
        weUser.setUserId(syncWeworkKfDto.getUserid());
        weUser.setIsActivate(1);
        List<WeUser> weUsers = weUserMapper.selectWeUserList(weUser);
        if (weUsers.size() > 0) {
            weUser = weUsers.get(0);
            if(weUser.getQrCode()!=null) {
                getWeworkQrUrlResDto.setQrUrl(weUser.getQrCode());
            } else {
                //临时获取二维码
                WeUserDto weUserDto = weUserClient.getUserByUserId(syncWeworkKfDto.getUserid());
                weUser.setQrCode(weUserDto.getQr_code());
                weUserMapper.updateWeUser(weUser);

                //添加二维码
                getWeworkQrUrlResDto.setQrUrl(weUserDto.getQr_code());
            }
        }

        return getWeworkQrUrlResDto;
    }

}
